  <?php
  	//error_reporting(E_ERROR);
  	require_once("../Scripts/DatabaseConnect.php");
	$con=Remoteconnect();
	if($con==null){					
		$con=Localconnect();						
	}
	
	//obtain get request data from charts.
	
	//These are the variables used for the chartDifShares.php
	$symbols=array();
	if(isset($_GET['symbols'])){
		$symbol=$_GET['symbols'];
		$symbols=explode(",",$symbol);
			
	}
	
	
	if(isset($_GET['startDate'])){
		$startDate=$_GET['startDate'];
			
		}
	
	
	if(isset($_GET['endDate'])){
			$endDate=$_GET['endDate'];
		}
		
	if(isset($_GET['type'])){
			$type=$_GET['type'];
		}		
		
	//These are the variables used for the chartIndicators.php, for reference the 'adv' refers to data being used for the chartIndicators.php.
	if(isset($_GET['advSymbol'])){
		$advSymbol=$_GET['advSymbol'];
		
			
	}
	
	if(isset($_GET['advType'])){
			$advType=$_GET['advType'];
		}	
	
	
	if(isset($_GET['advStartDate'])){
		$advStartDate=$_GET['advStartDate'];
		
			
		}
	
	
	if(isset($_GET['advEndDate'])){
			$advEndDate=$_GET['advEndDate'];
		}
	
	
	

	//data to be passed back to javascript charts:
	$dates=array();
	$advDates=array();
	$advSymbolArray=array();
	$symbolArray=array();
	$volumeArray=array();

	// obtain array of dates to send back to ChartDifShares.php	
	if(isset($_GET['startDate'])&isset($_GET['endDate'])){
		foreach($symbols as $symbol){
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$symbol' AND `Date` BETWEEN '$startDate' AND '$endDate'");
		
		
				while($row = mysqli_fetch_array($result)) {
					
					array_push($dates, $row['Date']);
					
		
				}	
	}
		$dates=array_unique($dates);//remove duplicated values
	}
	
	// obtain array of dates to send back to ChartIndicators.php		
	if(isset($_GET['advStartDate'])&isset($_GET['advEndDate'])){
		
	// obtain array of dates to send back to the javascript
	
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date` BETWEEN '$advStartDate' AND '$advEndDate'");
		
		
				while($row = mysqli_fetch_array($result)) {
					
					array_push($advDates, $row['Date']);
					
		
				}	
	}
	
//Obtain close price data for chartIndicators.php
if(isset($_GET['advType'])&&$_GET['advType']!="undefined"){
	foreach($advDates as $date){
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
				while($row = mysqli_fetch_array($result)) {
					
					array_push($advSymbolArray, $row[$advType]);
				}				
	}
}
		
	
				
	//obtain all of the prices for each symbol the user has selected for chartDifShares.php--Creating a multidimensional array
	$symbolData=array();
	$volumeData = array();
	
	//Percentage increase/decrease calcultion for chartDifShares.php
	if(isset($_GET['percentage'])&&$_GET['percentage']=="true"){
	if(isset($_GET['type'])&&$_GET['type']!=="undefined"){
		foreach($symbols as $symbol){
		$previous="";	
		$count=0;
		$q=1;
		$displaysDate=obtainDates(1,$con,$symbol,$startDate,$endDate);
		foreach($displaysDate as $date){
			
			$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$symbol' AND `Date`='$date'");
			
			
					while($row = mysqli_fetch_array($result)) {
						
						if($count>0){
							$calculation=$previous-$row[$type];
							$calculation=$calculation/$previous;
							$calculation=$calculation*100;
							array_push($symbolData, $calculation);
							array_push($volumeData, $row['Volume']);
						}
						$previous=$row[$type];
						$count+=1;
						
			
					}	
					
					
		}
			array_push($symbolArray,$symbolData);
			$symbolData=array();
			array_push($volumeArray,$volumeData);
			$volumeData=array();
		}
		}	
			
		
	}else{
	//Volume and Price of share data for each symbol user has selected for chartDifShares.php
	if(isset($_GET['type'])&&$_GET['type']!=="undefined"){
	foreach($symbols as $symbol){
		
	foreach($dates as $date){
		
	
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$symbol' AND `Date`='$date'");
		
		
				while($row = mysqli_fetch_array($result)) {
					
					array_push($symbolData, $row[$type]);
					array_push($volumeData, $row['Volume']);
	
				}	
		
	}
		array_push($symbolArray,$symbolData);
		$symbolData=array();
		array_push($volumeArray,$volumeData);
		$volumeData=array();
	}
	}
	}
	
	//Intialiseing of displays analysis arrays for chartIndicators.php
	$displays=array();
	$smaArray=array();
	$wmaArray=array();
	$emaArray=array();
	$sarArray=array();
	$bollingerArray=array();
	
	//get the displays arrays for chartIndicators.php
	if(isset($_GET['displays'])&&isset($_GET['advType'])&&$_GET['advType']!=="undefined"){//!=="undefined"){

		$displays=$_GET['displays'];

		$displays=explode(",",$displays);
		
		$smaData=array();
		$wmaData=array();
		$emaData=array();
		$sarDataHigh=array();
		$sarDataLow=array();
		$bollingerData=array();
		
		$closePriceData=array();
			foreach($displays as $display){
			
		
				$display=explode("/",$display);
				
				if($display[0]=="SMA"){
					
		
					foreach($display as $disp){
						if($disp!="SMA"){

					//calcualte SMA here
					$smaData=array();//reset Data to be null-neccessary when more than 1 symbol

					$displaysDate=obtainDates($disp,$con,$advSymbol,$advStartDate,$advEndDate);
					
					foreach($displaysDate as $date){
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($smaData, floatval($row[$advType]));
						
						}
							
					}
						array_push($smaArray, trader_sma($smaData, $disp));
						
						}
						
					}
					
					
					
				
				}
				
				if($display[0]=="WMA")	{				
		
					foreach($display as $disp){
						if($disp!="WMA"){

					
					//calcualte WMA here
					$smaData=array();//reset Data to be null-neccessary when more than 1 symbol

					$displaysDate=obtainDates($disp,$con,$advSymbol,$advStartDate,$advEndDate);
					
					foreach($displaysDate as $date){
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($wmaData, floatval($row[$advType]));
						
						}
							
					}
						array_push($wmaArray, trader_wma($wmaData, $disp));
						
						
						}
					}
					
					
					
				
				}
				
				if($display[0]=="EMA"){					
		
					foreach($display as $disp){
						if($disp!="EMA"){

					
					//calcualte EMA here
					$smaData=array();//reset Data to be null-neccessary when more than 1 symbol

					$displaysDate=obtainDates($disp,$con,$advSymbol,$advStartDate,$advEndDate);
					
					foreach($displaysDate as $date){
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($emaData, floatval($row[$advType]));
						
						}
							
					}
						array_push($emaArray, trader_ema($emaData, $disp));
						
						
						}
					}
					
					
					
				
				}
				
				if($display[0]=="SAR"){					
					$acceleration=0.02;
					$Maximum=0.2;
					
					//calcualte SAR here
					if(isset($display[1])){
						
						$acceleration=$display[1];
					}
					if(isset($display[2])){
						$Maximum=$display[2];
					}

					$displaysDate=obtainDates(1,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($displaysDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($sarDataHigh, floatval($row['High']));
							array_push($sarDataLow, floatval($row['Low']));
						
						}
							
					}
						
						
						
						array_push($sarArray, trader_sar($sarDataHigh,$sarDataLow, $acceleration,$Maximum));

				}
				
				if($display[0]=="Bollinger"){
					$SMA=20;
					$StandardDeviation=2;
					
					//calcualte SAR here
					if(isset($display[1])){
						
						$SMA=$display[1];
					}
					if(isset($display[2])){
						$StandardDeviation=$display[2];
					}
					

										
					$displaysDate=obtainDates($SMA,$con,$advSymbol,$advStartDate,$advEndDate);
					
					foreach($displaysDate as $date){
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($bollingerData, floatval($row[$advType]));
							
						
						}
							
					}
						
						
						array_push($bollingerArray, trader_bbands ($bollingerData, $SMA, $StandardDeviation, $StandardDeviation , TRADER_MA_TYPE_SMA));
						
					}
				
			
			
			
			}
		}
	
	//Initialse the technical analysis arrays for chartIndicator.php
	$RSIdata=array();
	$MacDdata=array();
	$ROCdata=array();
	$OBVdata=array();
	$OBVdata2=array();
	$WilliamRhighdata=array();
	$WilliamRlowdata=array();
	$WilliamRclosedata=array();
	$MTMdata=array();
	$SlowSTChighdata=array();
	$SlowSTClowdata=array();
	$SlowSTCclosedata=array();
	$FastSTChighdata=array();
	$FastSTClowdata=array();
	$FastSTCclosedata=array();
	$DMIplushighdata=array();
	$DMIpluslowdata=array();
	$DMIplusclosedata=array();
	$DMIminushighdata=array();
	$DMIminuslowdata=array();
	$DMIminusclosedata=array();
	$PVTdata=array();
	
	$RSIarray=array();
	$MacDarray=array();
	$ROCarray=array();
	$OBVarray=array();
	$WilliamRarray=array();
	$MTMarray=array();
	$SlowSTCarray=array();
	$FastSTCarray=array();
	$DMIarray=array();
	$PVTarray=array();
	
	//calculate the technical analysises for chartIndicator.php
	if(isset($_GET['technicals'])&&$_GET['technicals']!="undefined"){
		$indicators = $_GET['technicals'];
		
		$indicators=explode(",",$indicators);
		foreach($indicators as $indicator){
			
				$indicator=explode("/",$indicator);
				
				 

				if($indicator[0]=="RSI"){
					//callculate RSi
					$days=14;
					if(isset($indicator[1])){
							
						$days=$indicator[1];
					}	
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($RSIdata, floatval($row['Close']));
													
						}
							
					}

						
						array_push($RSIarray, trader_rsi($RSIdata, $days));

				}
					
				
				
				if($indicator[0]=="MacD"){
					//callculate MacD
					$fastperiod=12;
					$slowperiod=26;
					$signalperiod=9;
					if(isset($indicator[1])){
							
						$fastperiod=$indicator[1];
					}	
					if(isset($indicator[2])){
							
						$slowperiod=$indicator[2];
					}	
					if(isset($indicator[3])){
							
						$signalperiod=$indicator[3];
					}	
					$days=$slowperiod+$signalperiod-1;
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($MacDdata, floatval($row['Close']));
													
						}
							
					}
						
						
						
						array_push($MacDarray, trader_macd($MacDdata, $fastperiod,$slowperiod,$signalperiod));
						

				}
				
					
				
				
				if($indicator[0]=="ROC"){
					//callculate ROC
					$days=12;
					if(isset($indicator[1])){
							
						$days=$indicator[1];
					}	
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($ROCdata, floatval($row['Close']));
													
						}
							
					}
						
						
						
						array_push($ROCarray, trader_roc($ROCdata, $days));
					
				}
				
				if($indicator[0]=="OBV"){
					//callculate OBV			
					if(isset($indicator[1])){
							
						$days=$indicator[1];
					}	
					$indicatorsDate=obtainDates(1,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($OBVdata, floatval($row['Close']));
							array_push($OBVdata2, floatval($row['Volume']));
													
						}
							
					}
						
						
						
						array_push($OBVarray, trader_obv($OBVdata, $OBVdata2));
					
				}
				
				if($indicator[0]=="WilliamR"){
					
					//Calculate WilliamR
					$days=14;
					if(isset($indicator[1])){
							
						$days=$indicator[1];
					}	
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($WilliamRhighdata, floatval($row['High']));
							array_push($WilliamRlowdata, floatval($row['Low']));
							array_push($WilliamRclosedata, floatval($row['Close']));
													
						}
							
					}
						
						
						
						array_push($WilliamRarray, trader_willr($WilliamRhighdata,$WilliamRlowdata,$WilliamRclosedata, $days));
					
				}
				
				if($indicator[0]=="MTM"){
					//callculate MTM
					$days=9;
					if(isset($indicator[1])){
							
						$days=$indicator[1];
					}	
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($MTMdata, floatval($row['Close']));
													
						}
							
					}
						
						
						
						array_push($MTMarray, trader_mom($MTMdata, $days));
					
				}
				
				if($indicator[0]=="SlowSTC"){
					//callculate SlowSTC# NOT WORKING #
					echo"SlowSTC calculation Not currently working.";	
					$fastK=20;
					$fastD=5;
					$slowK=20;
					$slowD=5;
					
					if(isset($indicator[1])){
							
						$fastK=$indicator[1];
					}	
					if(isset($indicator[2])){
							
						$fastD=$indicator[2];
					}	
					$days = $fastK+$fastD-1;
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($SlowSTChighdata, floatval($row['High']));
							array_push($SlowSTClowdata, floatval($row['Low']));
							array_push($SlowSTCclosedata, floatval($row['Close']));
													
						}
							
					}
						
						
						
						array_push($SlowSTCarray, trader_stochf($SlowSTChighdata, $SlowSTClowdata, $SlowSTCclosedata, $fastK, $fastD, TRADER_MA_TYPE_SMA));
					
				}
				
				if($indicator[0]=="FastSTC"){
					//callculate FastSTC
					$fastK=20;
					$fastD=5;
					if(isset($indicator[1])){
							
						$fastK=$indicator[1];
					}	
					if(isset($indicator[2])){
							
						$fastD=$indicator[2];
					}	
					$days = $fastK+$fastD-1;
					$indicatorsDate=obtainDates($days,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($FastSTChighdata, floatval($row['High']));
							array_push($FastSTClowdata, floatval($row['Low']));
							array_push($FastSTCclosedata, floatval($row['Close']));
													
						}
							
					}
						array_push($FastSTCarray, trader_stochf($FastSTChighdata, $FastSTClowdata, $FastSTCclosedata, $fastK, $fastD, TRADER_MA_TYPE_SMA));
					
				}
				
				if($indicator[0]=="DMI"){
					//callculate DMI
					$days=14;
					if(isset($indicator[1])){
							
						$days=$indicator[1];
					}	
					
					$indicatorsDate=obtainDates($days+1,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
		
						while($row = mysqli_fetch_array($result)) {
							
							array_push($DMIplushighdata, floatval($row['High']));
							array_push($DMIpluslowdata, floatval($row['Low']));
							array_push($DMIplusclosedata, floatval($row['Close']));
							array_push($DMIminushighdata, floatval($row['High']));
							array_push($DMIminuslowdata, floatval($row['Low']));
							array_push($DMIminusclosedata, floatval($row['Close']));
													
						}
							
					}
						
						
						
						array_push($DMIarray, trader_minus_dm($DMIminushighdata,$DMIminuslowdata,$days));
						array_push($DMIarray, trader_plus_dm($DMIplushighdata,$DMIpluslowdata,$days));
					
				}
				
				if($indicator[0]=="PVT"){
					//callculate PVT
					$count=0;
					$yesturdaysClose="";
					$yesturdayPVT=0;
					$indicatorsDate=obtainDates(1,$con,$advSymbol,$advStartDate,$advEndDate);					
					foreach($indicatorsDate as $date){
						
						
						$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$advSymbol' AND `Date`='$date'");
		
							
						while($row = mysqli_fetch_array($result)) {
						$PVT=0;
						$close = $row['Close'];
						
						$volume = $row['Volume'];
						if($count>0){
							$PVT=0;
							$PVT=((($close-$yesturdaysClose)/$yesturdaysClose)*$volume)+$yesturdayPVT;
		
						array_push($PVTarray,$PVT);	
						
						}
						$count+=1;			
						$yesturdaysClose=$close;	
						$yesturdayPVT=$PVT;
						
								
						}
							
					}

					
				}

			
		}
	}
		
		


//Queries database to get the previous $number rows dates
function obtainDates($number,$con,$symbol,$startDate,$endDate){
	$dates=array();
	$newStartDate="";
	//get the correct starting date- eg.SMA: number dates to average
	
		$result = mysqli_query($con,"SELECT * FROM (
	SELECT * FROM `shares` WHERE `Symbol`='$symbol' AND `Date`<'$startDate' Order by `Date` DESC Limit $number
	)sub
	Order by `Date` asc limit 1
	");
		
		
				while($row = mysqli_fetch_array($result)) {
					$newStartDate=$row['Date'];
						
				
	// obtain array of dates to send back to the javascript
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$symbol' AND `Date` BETWEEN '$newStartDate' AND '$endDate'");
		
		
				while($row = mysqli_fetch_array($result)) {
					
					array_push($dates, $row['Date']);
					
		
				}	
	}
		$dates=array_unique($dates);//remove duplicated values
		return $dates;
}		
			
	
		
  ?>